﻿using System;
using System.Collections.Generic;
using System.Web.SessionState; //第一步：导入此命名空间
//ashx文件要使用Session,必须实现Session接口;
using System.Linq;
using System.Web;
using System.IO;
using HKEA.CMS.Core.Utility;

using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.BLL.Tbl;

namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// MediaRoomImg 的摘要说明
    /// </summary>
    public class MediaRoomImg : IHttpHandler, IRequiresSessionState //第二步：实现接口 到此就可以使用Session了
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            string type = context.Request.QueryString["type"];
            if (!string.IsNullOrEmpty(type))
            {
                switch (type)
                {
                    case "1"://获取一个MediaRoomRelated 的html
                        DeleteOneMediaRoom(context);
                        break;
                    case "2"://获取最后一个MediaRoom的html
                        GetLastMediaRoom(context);
                        break;
                    case "3"://获取指定UID的MediaRoom的html
                        GetOneMediaRoomByUID(context);
                        break;
                    default:

                        break;
                }
            }
        }

        public void GetLastMediaRoom(HttpContext context)
        {
            string ret_html = string.Empty;
            string ret = string.Empty;
            
            tblMediaRoom ent = new tblMediaRoom();
            ent = GetLastMediaRoom(); 
            if (ent != null)
            {
                string media_url = string.Format("{0}{1}", ent.FilePath, ent.FileName);
                ret_html = string.Format("<li id='mediaroom_{0}'><p class='p2'><a href='{1}_600_400.jpg'><img src='{2}_100_100.jpg' alt='' /></a></p><div class='p4'><span class='showbig'>View</span> | <span class='editBtnbig'><a href='javascript:updateMediaRoom({3});'>Edit</a></span> | <span class='closeBtnbig'><a href='javascript:delMediaRoom({4})'>Delete</a></span></div></li>", ent.MediaRoomUID, media_url, media_url, ent.MediaRoomUID, ent.MediaRoomUID);
                ret = ent.MediaRoomUID + "&" + ret_html;
            }

            context.Response.Write(ret);
        }

        public void GetOneMediaRoomByUID(HttpContext context)
        {
            string ret_html = string.Empty;
            string ret = string.Empty;
            string mediaroomUID = context.Request.QueryString["mediaroomUID"];
            tblMediaRoom ent = new tblMediaRoom();
            ent = tblMediaRoomBLL.GetInstance().GetEntity(int.Parse(mediaroomUID));
            if (ent != null)
            {
                string media_url = string.Format("{0}{1}", ent.FilePath, ent.FileName);
                ret_html = string.Format("<a href='{0}_600_400.jpg'><img src='{1}_100_100.jpg' alt='' /></a>", media_url,media_url);
                ret =  ret_html;
            }

            context.Response.Write(ret);
        }

        private void DeleteOneMediaRoom(HttpContext context)
        {
            #region 写入Loge
            string fileName = string.Empty;
            string customerUID = context.Request.QueryString["Cuid"];
            #endregion
            string ret = "0";//删除失败
            string mediaroomUID = context.Request.QueryString["mediaroomUID"];
            
            if (!string.IsNullOrEmpty(mediaroomUID))
            {
                int tmp_id = int.Parse(mediaroomUID);
                tblMediaRoomBLL bll = new tblMediaRoomBLL();
                tblMediaRoomRelatedBLL bllMRR = new tblMediaRoomRelatedBLL();
                tblMediaRoom_Related entMRR = new tblMediaRoom_Related();
                entMRR.MediaRoomUID = tmp_id;

                if (bllMRR.GetCountByENT(entMRR) > 0)
                {
                    ret = "2";//该图片有引用的
                }
                else
                {
                    string filePath = string.Empty;
                    tblMediaRoom entMR = bll.GetEntity(tmp_id);
                    if (entMR != null)
                    {
                        fileName = entMR.FileName;
                        filePath = string.Format("~{0}{1}",entMR.FilePath,entMR.FileName);                                     
                    }

                    tmp_id = bll.DeleteENT(entMR);
                    if (tmp_id > 0)
                    {
                        ret = "1";//删除成功1
                        if (!string.IsNullOrEmpty(filePath))
                        {
                            File.Delete(context.Server.MapPath(filePath + "_37_37.jpg"));
                            File.Delete(context.Server.MapPath(filePath + "_50_50.jpg"));
                            File.Delete(context.Server.MapPath(filePath + "_100_100.jpg"));
                            File.Delete(context.Server.MapPath(filePath + "_150_150.jpg"));
                            File.Delete(context.Server.MapPath(filePath + "_200_200.jpg"));
                            File.Delete(context.Server.MapPath(filePath + "_200_120.jpg"));
                            File.Delete(context.Server.MapPath(filePath + "_600_400.jpg"));
                        }
                    }
                }
                if ( Convert.ToInt32(ret) == 1) {
                    string info = string.Format("FileName:{0},have been delete",fileName);
                    WriteToLog(Convert.ToInt32(customerUID), info);
                }
            }
            context.Response.Write(ret);
        }

        private tblMediaRoom GetLastMediaRoom()
        {
            tblMediaRoom ret = new tblMediaRoom();
            ret = tblMediaRoomBLL.GetInstance().GetLastEntity();
            return ret;
        }

        protected void WriteToLog(int customerUID,string info)
        {
            BasePage page = new BasePage();            
            tblUser ent =page.GetManageFromSession();
            tblCustomer Company = tblCustomerBLL.GetInstance().GetEntity(customerUID);
            tblHistory entlog = new tblHistory();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 39;
            entlog.CustomerUID = customerUID;
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = ent.User_Name + " {" + Company.Name_Eng + "}{MediaRoomImg}" + info;
            entlog.LastModifyBy = ent.User_Name;
            int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}